import {Message} from "view-design";
import lodash from "lodash";

export function loadingPrompt(content){
    let msg = Message.loading({
        content: content,
        duration: 0
    });
    setTimeout(msg, 300000);
    return msg;
}
export function errorPrompt(content){
    Message.error({
        background:true,
        content:content,
        duration:10,
        closable:true
    });
}
export function successPrompt(content){
    Message.success({
        background:true,
        content:content,
        duration:2,
        closable:false
    });
}
export function forbidPrompt(content){
    Message.error({
        background:true,
        content:'不能对保留内容操作',
        duration:1.5,
        closable:false
    });
}
export function getOptions(arr,labelName,valueName){
    return arr.map(item=>{
        return {
            label:!!labelName?lodash.get(item,labelName):item,
            value:!!valueName?lodash.get(item,valueName):item,
        }
    })
}
export function getCascader(data,first,second){
    let tree=[];
    data.map(item=>{
        let index = tree.findIndex((m) => m.value === lodash.get(item,first));
        if (index === -1) {
            let obj = {
                label: lodash.get(item,first),
                value: lodash.get(item,first),
                children: [
                    {
                        label: lodash.get(item,second),
                        value: lodash.get(item,second),
                    },
                ],
            };
            tree.push(obj);
        } else {
            tree[index].children.push({
                label: lodash.get(item,second),
                value: lodash.get(item,second),
            });
        }
    });
    return tree;
}
export function getTree(data,first,second){
    let tree=[];
    data.map(item=>{
        let index = tree.findIndex((m) => m.value === lodash.get(item,first));
        if (index === -1) {
            let obj = {
                title: lodash.get(item,first),
                value: lodash.get(item,first),
                expand:true,
                children: [
                    {
                        title: lodash.get(item,second),
                        value: [lodash.get(item,first),lodash.get(item,second)],
                    },
                ],
            };
            tree.push(obj);
        } else {
            tree[index].children.push({
                title: lodash.get(item,second),
                value: [lodash.get(item,first),lodash.get(item,second)],
                expand:false,
            });
        }
    });
    return tree;
}
const iviewTool= {loadingPrompt,errorPrompt,successPrompt,forbidPrompt,getOptions,getCascader,getTree}
export default iviewTool;